﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace GMS.BIZ
{
    public class Computers
    {
        private string studentID;

        /// <summary>
        /// 初始化Computers
        /// </summary>
        /// <param name="studentID">学生学号</param>
        public Computers(string studentID)
        {
            this.studentID = studentID;
        }

        /// <summary>
        /// 数该学生有多少条计算机能力的记录
        /// </summary>
        public int Count()
        {
            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            DataSet ds = optGComputer.GetList("std_id='" + this.studentID + "'");
            try
            {
                return ds.Tables[0].Rows.Count;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 添加一条学生计算机能力
        /// </summary>
        /// <param name="inputComputer">计算机能力对象</param>
        public string Add(Computer inputComputer)
        {
            GMS.ORM.Model.GComputer myGComputer = new GMS.ORM.Model.GComputer();
            myGComputer.std_id = this.studentID;
            myGComputer.computer_level = inputComputer.computer_level;
            myGComputer.computer_time = inputComputer.computer_time;

            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            bool ok = optGComputer.Add(myGComputer);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 删除一条学生英语能力
        /// </summary>
        /// <param name="id">删除的id号</param>
        public string Delete(decimal id)
        {
            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            bool ok = optGComputer.Delete(id);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 更新一条学生英语能力
        /// </summary>
        /// <param name="inputComputer">计算机能力的对象</param>
        public string Update(Computer inputComputer)
        {
            GMS.ORM.Model.GComputer myGComputer = new GMS.ORM.Model.GComputer();
            myGComputer.id = inputComputer.id;
            myGComputer.std_id = this.studentID;
            myGComputer.computer_level = inputComputer.computer_level;
            myGComputer.computer_time = inputComputer.computer_time;

            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            bool ok = optGComputer.Update(myGComputer);
            if (!ok) return ErrorMessage.DBOptError;
            else return ErrorMessage.OK;
        }

        /// <summary>
        /// 获得一条学生的计算机能力
        /// </summary>
        /// <param name="id">id</param>
        public Computer GetOne(decimal id)
        {
            Computer outputComputer = new Computer();
            GMS.ORM.Model.GComputer myGComputer = new GMS.ORM.Model.GComputer();
            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            try
            {
                myGComputer = optGComputer.GetModel(id);
                outputComputer.id = myGComputer.id;
                outputComputer.std_id = myGComputer.std_id;
                outputComputer.computer_level = myGComputer.computer_level;
                outputComputer.computer_time = myGComputer.computer_time;
                return outputComputer;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获得一个学生的计算机能力的数组
        /// </summary>
        public ArrayList GetList()
        {
            ArrayList returnList = new ArrayList();

            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            DataSet ds = optGComputer.GetList("std_id='" + this.studentID + "' ORDER BY computer_time DESC");
            if (ds.Tables[0].Rows.Count == 0)
                return returnList;
            else
            {
                int howMany = ds.Tables[0].Rows.Count;
                for (int i = 0; i < howMany; i++)
                    returnList.Add(decimal.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                return returnList;
            }
        }

        /// <summary>
        /// 获得一个学生的计算机能力的DataSet
        /// </summary>
        public DataSet GetDataSet()
        {
            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            DataSet ds = optGComputer.GetList("std_id='" + this.studentID + "' ORDER BY computer_time DESC");
            return ds;
        }

        /// <summary>
        /// 返回学生的计算机能力
        /// </summary>
        /// <param name="studentID">学生学号，可以为空</param>
        /// <param name="computer_level">计算机能力，可以为空</param>
        /// <param name="after">在此时间之后</param>
        /// <param name="before">在此时间之前</param>
        static public DataSet GetDataSet(string studentID, string computer_level, DateTime? after, DateTime? before)
        {
            string queryString = "1=1";
            if (studentID != string.Empty) queryString = queryString + " AND std_id='" + studentID + "'";
            if (computer_level != string.Empty) queryString = queryString + " AND computer_level='" + computer_level + "'";
            if (after != null) queryString = queryString + " AND computer_time>" + after.ToString();
            if (before != null) queryString = queryString + " AND computer_time<" + before.ToString();

            GMS.ORM.DAL.GComputer optGComputer = new GMS.ORM.DAL.GComputer();
            return optGComputer.GetList(queryString + " ORDER BY computer_time");
        }
    }
}
